<?php if (!defined('DB_PEREFIX')) exit;

global $_POST, $db, $GLOBAL_USER; 

$id = intval ($_POST['id']);
if ($_POST['value'] == 0) $bad = true;
if ($_POST['value'] == 1) $good = true;

$get_art = $db->query ("SELECT art_rating, art_voters FROM ".DB_PEREFIX."_articles WHERE art_id = '$id'");
if ($db->num_rows ($get_art) == 1) {
	$art = $db->fetch_array ($get_art);
	
	$vote = false;
	$voters = explode (",", $art['art_voters']);
	foreach ($voters as $v) if ($GLOBAL_USER['user_id'] == trim($v)) $vote = true;
	
	if ($art['art_voters'] == "") $new_voters = $GLOBAL_USER['user_id'];
	else $new_voters = $art['art_voters'].",".$GLOBAL_USER['user_id'];
	
	$rating = $art['art_rating'];
	if ($vote == false) {
		if ($bad == true) $rating -= 1;
		if ($good == true) $rating += 1;
		$db->query ("UPDATE ".DB_PEREFIX."_articles SET art_rating = '$rating', art_voters = '$new_voters' WHERE art_id = '$id'");
	}
	//print number_format($rating, 2, '.', '');
	($rating == 0) 
		? $rating = "0 баллов" 
		: $rating = count_smth ($rating, array ("балл", "балла", "баллов"));
	print $rating;
}

// TODO: Стоит вернуть и выключеный стрелки
exit;
?>
